package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Build;
import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.build.BuildLevelUpTemplate;
import com.xcity.game.build.BuildTemplate;
import com.xcity.game.build.BuildUtils;
import com.xcity.game.item.ItemInfo;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class BuildStatusResponse extends Response {
	
	public int buildId;
	public int health;
	public int id;
	public String img;
	public int income;
	public int incomeAdded; // 每分钟加成收入(可为负，受世界事件影响)
	public int level;
	public int maxIncome;
	public int money; // 累计收益
	public String name;
	public int price; // 出售价格
	public int remainingTime; // 剩余cd
	public int cdTime; // 总cd
	public byte status; // 0:建造/升级中 ; 1:空闲状态; 2:休店期
	public long restCD; // 休店期剩余cd
	public AttackerInfo attacker = AttackerInfo.EMPTY;
	public int slot;
	public int maxSlot;
	public int defense;
	public int reserve;
	public int cure;
	public List<com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo> employees = Collections.emptyList();
	public byte canLvlUp; // 是否可升级（1：是，0：否；材料充足则可升级）
	
	public static class AttackerInfo {
		public long id;
		public String name;
		public String icon;
		public static final AttackerInfo EMPTY = new AttackerInfo();
		public AttackerInfo() {}
		public AttackerInfo(Player player) {
			this.id = player.getId();
			this.name = player.getName();
			this.icon = player.getIcon();
		}
	}
	
	public static class EmployeeSimpleInfo extends com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo {
		public float defense;
		public EmployeeSimpleInfo(Employee e) {
			super(e);
			this.defense = e.getDefense();
		}
		public EmployeeSimpleInfo() {
		}
	}
	
	public BuildStatusResponse(int serial, Player player, Build ref) {
		super(serial);
		BuildTemplate temp = ref.getTemplate();
		BuildLevelUpTemplate lvlTemp = temp.getLevelUpTemplate(ref.getLevel());
		buildId = ref.getId();
		health = lvlTemp.getRisk();
		id = ref.getId();
		img = temp.getImage(ref.getLevel());
		int incomePerMinute = ref.getIncomePerMinute();
		income = /*lvlTemp.getIncome()*/incomePerMinute;
		incomeAdded = (int) (/*lvlTemp.getIncome()*/incomePerMinute * ref.getFactor() - /*lvlTemp.getIncome()*/incomePerMinute);
		level = ref.getLevel();
		maxIncome = /*lvlTemp.getMaxIncome()*/ref.getMaxIncome();
		money = ref.settle();
		name = temp.getName(ref.getLevel());
		price = lvlTemp.getPrice();
		remainingTime = ref.getLeftCD();
		cdTime = ref.getNextLevelTemplate().getCd() * 1000;
		status = ref.getState().codeOfOldVersion();
		restCD = ref.getLeftRestCD();
		slot = ref.getSlot();
		maxSlot = Build.MAX_SLOT;
		defense = (int) ref.getDefense();
		reserve = ref.calcReserve();
		cure = ref.getCure();
		long[] employeeIds = ref.employees();
		Player owner = ref.owner(); // owner re get
		if (ref.getAttackerId() != 0L) {
			attacker = new AttackerInfo(owner.getId() == ref.getAttackerId() ? owner : ref.attacker());
		}
		if (employeeIds != null) {
			employees = new ArrayList<com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo>(employeeIds.length);
			for (long id : employeeIds) {
				Employee e;
				if (id != 0 && (e = owner.getEmployees().get(id)) != null) {
					employees.add(new EmployeeSimpleInfo(e));
				} else {
					employees.add(EmployeeSimpleInfo.EMPTY);
				}
			}
		}
		if (ref.getLevel() < BuildUtils.getMaxLevel(ref)) {
			BuildLevelUpTemplate lvlUpTemplate = ref.getNextLevelTemplate();
			if (itemsEnough(player, lvlUpTemplate.getNeedItems())) {
				canLvlUp = 1;
			}
		}
	}
	
	static boolean itemsEnough(Player player, ItemInfo[] items) {
		if (items == null || items.length == 0) {
			return true;
		}
		for (ItemInfo item : items) {
			if (!player.getBag().hasEnough(item.getId(), item.getCount())) {
				return false;
			}
		}
		return true;
	}
	
}
